package com.springboot.util;

import com.alibaba.fastjson.JSONObject;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

public class TestPost {
    public static void main(String[] args) {
        try{
            HttpURLConnection conn=null;
            URL url = new URL ("http://220.220.220.9/ZhiFang.ReportFormQueryPrint/serviceWCF/ReportFormWebService.asmx");

            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setConnectTimeout(800000);
            conn.setReadTimeout(800000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
            String input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "  <soap12:Body>\n" +
                    "    <GetReportFormPDFByFields_Base64String xmlns=\"http://tempuri.org/\">\n" +
                    "      <fields>\n" +
                    "        <string>SerialNo</string>\n" +
                    "      </fields>\n" +
                    "      <values>\n" +
                    "        <string>2308080360</string>\n" +
                    "      </values>\n" +
                    "      <order>\n" +
                    "      </order>\n" +
                    "      <RangeDay>30</RangeDay>\n" +
                    "      <IsPrintTimes>0</IsPrintTimes>\n" +
                    "      <flag>0</flag>\n" +
                    "    </GetReportFormPDFByFields_Base64String>\n" +
                    "  </soap12:Body>\n" +
                    "</soap12:Envelope>";
            System.out.println("请求志方入参："+input);
            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes("utf-8"));
            os.flush();
            InputStream in=conn.getInputStream();
            System.out.println("code:"+conn.getResponseCode());
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    in,"utf-8"));
            String output;

            String tpresult = "";

            while ((output = br.readLine()) != null) {

                tpresult+=output;

                jxXml(tpresult);
            }

            System.out.println(tpresult);
        }catch (Exception e){
            e.printStackTrace();
        }
//        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "<soap:Envelope\n" +
//                "    xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"\n" +
//                "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
//                "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
//                "    <soap:Body>\n" +
//                "        <GetReportFormPDFByFields_Base64StringResponse\n" +
//                "            xmlns=\"http://tempuri.org/\">\n" +
//                "            <GetReportFormPDFByFields_Base64StringResult>true</GetReportFormPDFByFields_Base64StringResult>\n" +
//                "            <ReportFormPdfPathBtis>\n" +
//                "                <string>1212</string>\n" +
//                "            </ReportFormPdfPathBtis>\n" +
//                "            <ErrorInfo />\n" +
//                "        </GetReportFormPDFByFields_Base64StringResponse>\n" +
//                "    </soap:Body>\n" +
//                "</soap:Envelope>";


    }

    public static String jxXml(String xml){

        if(xml.indexOf("<string>")!= -1){
            String temp = xml.substring(xml.indexOf("<string>")+8,xml.indexOf("</string>"));
            System.out.println(temp);
            return temp;
        }else{
            return  "";
        }

    }
}
